package net.sf.midollo.net.io;

import java.io.IOException;
import java.net.Socket;
import java.net.URL;

import net.sf.midollo.io.IOStream;
import net.sf.midollo.io.IOStreamFactory;

public class SocketIOStreamFactory implements IOStreamFactory {

  protected final String host;
  protected final int port;

  public SocketIOStreamFactory(URL url, int defaultPort) {
    String host = url.getHost();
    if (host == null || host.length() == 0)
      host = "127.0.0.1";
    int port = url.getPort();
    if (port <= 0)
      port = defaultPort;
    this.host = host;
    this.port = port;
  }

  public SocketIOStreamFactory(String host, int port) {
    this.host = host;
    this.port = port;
  }

  public IOStream open() throws IOException {
    Socket socket = new Socket(this.host, this.port);
    return new SocketIOStream(socket);
  }
}
